using System;
using System.Collections.Generic;
using System.Text;

using Mapack;

namespace A4G.Core
{
	[Serializable]
	public class ScoringMatrix : Matrix
	{
		private readonly Dictionary<char, int> _symbolIndexes;

		public ScoringMatrix()
			: base()
		{
		}

		public ScoringMatrix(char[] symbols)
			: this(symbols, 0.0)
		{
		}

		public ScoringMatrix(char[] symbols, double value)
			: base(symbols.Length, symbols.Length, value)
		{
			_symbols = symbols;

			_symbolIndexes = new Dictionary<char, int>(Symbols.Length);
			int index = 0;
			foreach (char symbol in Symbols)
			{
				_symbolIndexes.Add(symbol, index++);
			}
		}

		private char[] _symbols;
		public char[] Symbols
		{
			get { return _symbols; }
			set { _symbols = value; }
		}

		public double this[char i, char j]
		{
			set
			{
				this.data[_symbolIndexes[i]][_symbolIndexes[j]] = value;
			}
			get
			{
				return this.data[_symbolIndexes[i]][_symbolIndexes[j]];
			}
		}

		public void SetAsIdentityMatrix()
		{
			Reset();
			SetDiagonalValues(1);
		}

		public void SetDiagonalValues(double diagonalValue)
		{
			foreach (char symbol in Symbols)
			{
				this[symbol, symbol] = 1.0;
			}
		}

		public void SetNonDiagonalValues(double nonDiagonalValue)
		{
			foreach (char symbol1 in Symbols)
			{
				foreach (char symbol2 in Symbols)
				{
					if (symbol1 != symbol2)
					{
						this[symbol1, symbol2] = nonDiagonalValue;
					}
				}
			}
		}
	}
}
